/* coreFunction.c */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "prototypes.h"
#include "typedefs.h"
#include "macros.h"

/**
* help
*
*   calls the help menu
*
*/
void help (int argc, char* argv[])
{
    int helpRequested=0;
    for(int i=0;i<argc;i++)
    {
        if(!strcmp(argv[i],"-help"))
        {
            helpRequested=1;
        }
    }
    if(!helpRequested)
    {
        printf("\nInvalid use of imageToAscii!\n");
    }
    else
    {
        printf("\nYou requested help...here I am!\n");
    }
    printf("\nUsage for picture: \n imageToAscii [filePath (3 to 255 chars)] [options]");
    printf("\nOptions (Standard: Monochrome ASCII-Image):");
    printf("\n\t -p \t\t\t\t Enables picture mode (Required)");
    printf("\n\t -c \t\t\t\t Use Colored Pixels\n");        //TODO
    printf("\nFor video:");
    printf("\n imageToAscii [folderPath (3 to %d chars)] [format (3 to %d chars)] [options]",MAX_FOLDER-1,MAX_FORMAT-1);
    printf("\nOptions (Standard: Monochrome ASCII-Pixel):");
    printf("\n\t -v \t\t\t\t Enables video mode (Required)");
    printf("\n\t -c \t\t\t\t Use Colored Pixels");
    printf("\n\t -l \t\t\t\t Loop Video");
    printf("\n\t ## \t\t\t\t Number of pictures in your video\n\n");
}

/**
* parseArguments
*
*   Parses input arguments
*
* @param    argc            number of arguments
*           argv[]          string of argument[]
* @return   -1              calls help function
*           0               finished successfully
*/
int parseArguments(int argc, char* argv[], Size* consoleSize)
{
    if(argc<3||argc>7)
    {
        return -1;
    }
    for(int i=1;i<argc;i++)
    {
        if(!strcmp(argv[i],"-help"))
        {
            return -1;
        }
        if(!strcmp(argv[i],"-p"))
        {
            if(argc>4)
            {
                return -1;
            }
            for(int j=1;j<argc;j++)
            {
                if(!strcmp(argv[j],"-c"))
                {
                    for(int k=1;k<argc;k++)
                    {
                        if(k==j||k==i)
                        {
                        k++;
                        }
                        if(strlen(argv[k])>2)
                        {
                            if(printImageToConsole(consoleSize, coloredPixel, argv[k]) == -1)
                            {
                                return -1;
                            }
                            return 0;
                        }
                    }
                }
            }
            if(argc>3)
            {
                return -1;
            }
            for(int l=1;l<argc;l++)
            {
                if(l==i)
                {
                    l++;
                }
                if(printImageToConsole(consoleSize, monochromeAscii, argv[l]) == -1)
                {
                    return -1;
                }
                return 0;
            }
        }
    }
    enum conversionOption mode = monochromeAscii;
    enum conversionOption loopMode = noLoop;
    int argVideo, argMode, argLoop, argFormat, argFolder, frameCount;
    char* folder=(char *)malloc(MAX_FOLDER*sizeof(char));
    char* format=(char *)malloc(MAX_FORMAT*sizeof(char));
    if(folder==NULL||format==NULL)
    {
        printf("\nAllocation Failure!\n");
        return -2;
    }
    folder="a";
    format="a";
    argMode=-1;
    argLoop=-1;
    argVideo=-1;
    argFormat=-1;
    argFolder=-1;
    frameCount=-1;
    for(int i=1;i<argc;i++)
    {
        if(!strcmp(argv[i],"-v"))
        {
            #ifdef DEBUG_OUTPUT
            printf("Video mode enabled.");
            #endif
            argVideo=i;
            for(int j=1;j<argc;j++)
            {
                if(!strcmp(argv[j],"-c"))
                {
                    mode = coloredPixel;
                    argMode=j;
                }
                if(!strcmp(argv[j],"-l"))
                {
                    loopMode = loop;
                    argLoop=j;
                }
            }
            for(int k=1;k<argc;k++)
            {
                if((k==argVideo||k==argMode||k==argLoop)&&k<argc-1)
                {
                    k++;
                }
                while(strlen(argv[k])>2&&(argFormat-1||argFolder==-1))
                {
                    if(strstr(argv[k],".bmp"))
                    {
                        if(strlen(argv[k])>=MAX_FORMAT-1)
                        {
                            return -1;
                        }
                        format=argv[k];
                        argFormat=k;
                    }
                    else
                    {
                        if(strlen(argv[k])>=MAX_FOLDER-1)
                        {
                            return -1;
                        }
                        folder=argv[k];
                        argFolder=k;
                    }
                    if(k<argc-1)
                    {
                        k++;
                    }
                    else
                    {
                        break;
                    }
                }
                for(int l=1;l<=100&&frameCount==-1;l++)
                {
                    if(atoi(argv[k])==l)
                    {
                        frameCount=l;
                    }
                }
            }
            if(strcmp(folder,"a")==0||strcmp(format,"a")==0||frameCount==-1)
            {
                return -1;
            }
            runVideo(folder, format, frameCount, 2, loopMode, mode);
            return 0;
        }
    }
    return -1;
}

/**
* printImageToConsole
*
*   Loading image, processing image, converting image, printing image to console
*
* @param    consoleSize     console size
*           type            type of conversion
* @return   -1              calls help function
*           0               finished successfully
*/
int     printImageToConsole(Size* consoleSize, enum conversionOption type, char* filepath)
{
    Image* image;
    char* asciiImage;
    switch (type)
    {
        case coloredPixel: image = getImage(filepath);
                if(!image)
                {
                    return -1;
                }
                asciiImage=transformImageToAsciiPixel(image, consoleSize);
                break;
        case monochromeAscii: image = getImage(filepath);
                if(!image)
                {
                    return -1;
                }
                asciiImage=transformImageToAsciiArtMonochrome(image, consoleSize);
                break;
        default: return -1;
                 break;
    }
    putchar('\n');
    fputs(asciiImage,stdout);
    fputs("\x1b[39;49mEnde\n",stdout);
    return 0;
}
